/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import newkfp.BillExporterInformation;
import newkfp.BillInformation;
import newkfp.Quantity_Number;

/**
 *
 * @author labib.farag
 */
@ManagedBean(name = "calculationController")
@ViewScoped
public class CalculationController {

    private ArrayList<Quantity_Number> orderList = new ArrayList<Quantity_Number>();
    private double quantity;
    private int number_quantity;
    private double totalRow;
    private boolean libra;
    private double totalKg;
    private double totalLbr;
    @ManagedProperty(value = "#{billInformationController}")
    BillInformation billInformation;
    @ManagedProperty(value = "#{billExporterInformationController}")
    BillExporterInformation billExporterInformation;
    private double total_Number;
    private double total_Number2;

    public double getTotal_Number2() {
        return total_Number2;
    }

    public void setTotal_Number2(double total_Number2) {
        this.total_Number2 = total_Number2;
    }

    public BillInformation getBillInformation() {
        return billInformation;
    }

    public void setBillInformation(BillInformation billInformation) {
        this.billInformation = billInformation;
        total_Number = billInformation.getTotal_Number();
        System.out.println("done" + total_Number);
    }

    public BillExporterInformation getBillExporterInformation() {
        return billExporterInformation;
    }

    public void setBillExporterInformation(BillExporterInformation billExporterInformation) {
        this.billExporterInformation = billExporterInformation;
        total_Number2 = billExporterInformation.getTotal_Number();
    }

    /**
     * @return the orderList
     */
    public ArrayList<Quantity_Number> getOrderList() {
        return orderList;
    }

    public double getTotalLbr() {
        if (libra) {
            totalLbr = totalKg * 0.454;
        } else {
            totalLbr = totalKg;
        }
        return totalLbr;
    }

    public void setTotalLbr(double totalLbr) {
        this.totalLbr = totalLbr;
    }

    public double getTotal_Number() {
        
        System.out.println("again ="+total_Number);
        
        return total_Number;
//        System.out.println(getBillInformation().getTotal_Number());
//        return getBillInformation().getTotal_Number();
    }

    public void setTotal_Number(double total_Number) {
        this.total_Number = total_Number;
    }

    /**
     * @param orderList the orderList to set
     */
    public void setOrderList(ArrayList<Quantity_Number> orderList) {
        this.orderList = orderList;
    }

    /**
     * @return the quantity
     */
    public double getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    /**
     * @return the number_quantity
     */
    public int getNumber_quantity() {
        return number_quantity;
    }

    /**
     * @param number_quantity the number_quantity to set
     */
    public void setNumber_quantity(int number_quantity) {
        this.number_quantity = number_quantity;
    }

    /**
     * @return the totalRow
     */
    public double getTotalRow() {
        return quantity * number_quantity;
    }

    /**
     * @param totalRow the totalRow to set
     */
    public void setTotalRow(double totalRow) {
        this.totalRow = totalRow;
    }

    public String addAction() {


        int totalAdded = 0;
        for (Iterator<Quantity_Number> it = orderList.iterator(); it.hasNext();) {
            Quantity_Number qu = it.next();
            totalAdded += qu.getQuantity();


        }


        totalAdded += quantity;


//        System.out.println(" total added = " + totalAdded + " TOTAL NUMBER IS = " + billInformation.getTotal_Number());
        //if (!(totalAdded > billInformation.getTotal_Number())) {
        if (total_Number > 0 && total_Number >= number_quantity) {
            totalKg += getTotalRow();
            Quantity_Number order = new Quantity_Number(quantity, number_quantity, getTotalRow(), getTotalKg());
            orderList.add(order);
            total_Number = total_Number - number_quantity;

        }



        return null;
    }

    public String calLbr() {
        totalLbr = totalKg * 0.455;
        return null;
    }

    public String addActionExporter() {

        int totalAdded = 0;
        System.out.println("orderList :" + orderList.size());
        for (Iterator<Quantity_Number> it = orderList.iterator(); it.hasNext();) {
            Quantity_Number qu = it.next();
            totalAdded += qu.getQuantity();


        }


        totalAdded += quantity;


        System.out.println(" total added = " + totalAdded + "TOTAL NUMBER IS = " + billExporterInformation.getTotal_Number());
        //if (!(totalAdded > billInformation.getTotal_Number())) {
        if (total_Number2 > 0 && total_Number2 >= number_quantity) {
            totalKg += getTotalRow();
            Quantity_Number order = new Quantity_Number(quantity, number_quantity, getTotalRow(), getTotalKg());
            orderList.add(order);
            total_Number2 = total_Number2 - number_quantity;

        }



        return null;
    }

    public boolean isLibra() {
        return libra;
    }

    public void setLibra(boolean libra) {
        this.libra = libra;
    }

    /**
     * @return the totalKg
     */
    public double getTotalKg() {
        return totalKg;
    }

    /**
     * @param totalKg the totalKg to set
     */
    public void setTotalKg(double totalKg) {
        this.totalKg = totalKg;
    }

    public String finishCalculation() {
        billInformation.setTotalKg(totalLbr);
        return "CustomerBill?faces-redirect=true";
    }

    public String finishExporterCalculation() {
        billExporterInformation.setTotalKg(totalLbr);
        return "ExporterBill?faces-redirect=true";
    }
}
